package com.shiguo365.market.display.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.shiguo365.market.R;
import com.shiguo365.market.application.MyApplication;
import com.shiguo365.market.display.base.BaseActivity;
import com.shiguo365.market.display.listener.NavigationFinishClickListener;
import com.shiguo365.market.display.util.ToastUtils;
import com.shiguo365.market.display.view.IAddressAddView;
import com.shiguo365.market.presenter.contract.IAddressAddPresenter;
import com.shiguo365.market.presenter.implement.AddressAddPresenter;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddressAddActivity extends BaseActivity implements IAddressAddView {


    private static final String TAG = "CartActivity";
    private IAddressAddPresenter mAddressAddPresenter;



    @BindView(R.id.address_add_toolbar)
    protected Toolbar toolbar;

    @BindView(R.id.buttonAddressAdd)
    protected Button mbtnAdd;

    @BindView(R.id.editTextAddressShopName)
    protected EditText editTextAddressShopName;

    @BindView(R.id.editTextAddressName)
    protected EditText editTextAddressName;

    @BindView(R.id.editTextAddressPhone)
    protected EditText editTextAddressPhone;

    @BindView(R.id.spinnerAddressProvince)
    protected Spinner spinnerAddressProvince;

    @BindView(R.id.spinnerAddressCity)
    protected Spinner spinnerAddressCity;

    @BindView(R.id.editTextAddressAddress)
    protected EditText editTextAddressAddress;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_add);
        ButterKnife.bind(this); //ButterKnife初始化
        toolbar.setNavigationOnClickListener(new NavigationFinishClickListener(this));
        mAddressAddPresenter=new AddressAddPresenter(this,this);
    }



    @OnClick(R.id.buttonAddressAdd)
    public void addressAdd(){
        Map address = new HashMap<String,String>();
        address.put("uid", MyApplication.getUID());
        address.put("shopename",editTextAddressShopName.getText());
        address.put("name",editTextAddressName.getText());
        address.put("phone",editTextAddressPhone.getText());
        address.put("province",spinnerAddressProvince.getSelectedItem().toString());
        address.put("city",spinnerAddressCity.getSelectedItem().toString());
        address.put("street",editTextAddressAddress.getText());
        mAddressAddPresenter.addAddress(MyApplication.getUID(),address);
    }

    @Override
    public void OnAddAddressOk() {
        //地址添加成功
        new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage("您的地址添加成功！")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent intent=new Intent();
                        intent.putExtra("data_return","Hello");
                        setResult(RESULT_OK,intent);
                        finish();
                    }
                })
                .setCancelable(false)
                .show();
    }
}
